package cn.szzsi._web;

import cn.szzsi.api.oauth.OauthQQ;
import cn.szzsi.api.util.TokenUtil;
import cn.szzsi.common.kit.CommonUtil;
import cn.szzsi.common.kit.WikiQqUtil;

import cn.szzsi.common.model.Member;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import javafx.scene.control.Skin;
import org.apache.commons.lang3.StringUtils;
import org.json.HTTP;

import cn.szzsi._Config;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ThirdpartyController extends Controller{

    String backUrl=_Config.p.get("mall_web.url")+"/b/thirdparty";
    /**
     * 获取Authorization Code
     */
    public void authorize(){
        String site=getPara("site");
        String shopId=getPara("shopId");
        if("qq".equals(site)){//qq
            String redirect_uri=URLEncoder.encode(backUrl);
            boolean flag= CommonUtil.isMobileDevice(getRequest());
            String url="";
            if(flag){
                url= WikiQqUtil.authorizeURL+"?response_type=code&client_id=101424147&redirect_uri="+redirect_uri+"&display=mobile&state="+shopId;
            }else{
                url= WikiQqUtil.authorizeURL+"?response_type=code&client_id=101424147&redirect_uri="+redirect_uri+"&state="+shopId;
            }
            redirect(url);
        }

    }

    public void index(){
        String code=getPara("code");
        String shopId=getPara("state");
        System.out.println("code------------"+code);
        System.out.println("shopId------------"+shopId);
        String redirect_uri=URLEncoder.encode(backUrl);
        if(StrKit.notBlank(code)){

            //access_token为空,根据传入的code去获取access_token
            String pcUrl=WikiQqUtil.accessTokenURL+"?grant_type=authorization_code&client_id=101424147&client_secret=c182a1e794b841046cffc6acf9e65105&code="+code+"&redirect_uri="+redirect_uri;
            String result= HttpKit.get(pcUrl);
            if(result.contains("access_token")){
                String jsondata= StringUtils.replace(StringUtils.replace(result,"&",","),"=",":");
                jsondata="{"+jsondata+"}";
                org.json.JSONObject orgJson= new org.json.JSONObject(jsondata);
                JSONObject jsonObject= JSON.parseObject(orgJson.toString());
                String access_token=jsonObject.getString("access_token");
                String expires_in=jsonObject.getString("expires_in");
                String refresh_token=jsonObject.getString("refresh_token");
                System.out.println("access_token-------------"+access_token);
                System.out.println("expires_in-------------"+expires_in);
                System.out.println("refresh_token-------------"+refresh_token);
                String openidResult=HttpKit.get(WikiQqUtil.getOpenIDURL+"?access_token="+access_token);
                System.out.println("openidResult-------"+openidResult);
                Integer start=openidResult.indexOf("{");
                Integer end=openidResult.lastIndexOf("}")+1;
                JSONObject openidJson=JSON.parseObject(openidResult.substring(start,end));
                String openid=openidJson.getString("openid");
                System.out.println("openid------------"+openid);
                Member member = Member.dao.isExistByOpenid(openid);//查看该用户是已经授权
                if(member!=null){//已经授权
                    //member.setUsername();
                }else{//没有授权

                }

            }



        }



    }

    /**
     * 时间戳转换成日期字符串
     * @param timestamp
     * @return
     */
    public static String timestampToDateTimeStr(Long timestamp){
        SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateTimeStr=sdf.format(new Date(timestamp));
        return dateTimeStr;

    }

    public static void main(String[] args) {

        JSONObject jsonObject= OauthQQ.getUserInfo("101424147","4D04AA3412D8868773CAB8ADF4F90847","72533DAB55D674E4631A4D42600B02F2");
        System.out.println(jsonObject);
        String nickname=jsonObject.getString("nickname");
        String gender=jsonObject.getString("gender");
        String province=jsonObject.getString("province");
        String city=jsonObject.getString("city");
        String year=jsonObject.getString("year");
        String figureurl_qq_2=jsonObject.getString("figureurl_qq_2");
        System.out.println("nickname----------"+nickname);
        System.out.println("gender----------"+gender);
        System.out.println("province----------"+province);
        System.out.println("city----------"+city);
        System.out.println("year----------"+year);
        System.out.println("figureurl_qq_2----------"+figureurl_qq_2);

        String str="access_token=FE04************************CCE2&expires_in=7776000&refresh_token=88E4************************BE14";
        System.out.println(TokenUtil.getAccessToken(str));


    }



}
